﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfBinding
{
    /// <summary>
    /// CollectionViewTest.xaml 的交互逻辑
    /// </summary>
    public partial class CollectionViewTest : Page
    {
        TestModels models;
        CollectionView view;
        public CollectionViewTest()
        {
            InitializeComponent();
            models = new TestModels();
            this.listBox1.ItemsSource = models;
            view = (CollectionView)CollectionViewSource.GetDefaultView(listBox1.ItemsSource);
            listBox1.IsSynchronizedWithCurrentItem = true;
        }

        bool isAsce = true;
        private void btnSort_Click(object sender, RoutedEventArgs e)
        {
            view.SortDescriptions.Clear();
            if (isAsce)
                view.SortDescriptions.Add(new SortDescription("Age", ListSortDirection.Ascending));
            else
                view.SortDescriptions.Add(new SortDescription("Age", ListSortDirection.Descending));
            isAsce = !isAsce;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            view.GroupDescriptions.Clear();
            if (isAsce)
                view.GroupDescriptions.Add(new PropertyGroupDescription("City"));
            else
                view.GroupDescriptions.Clear();

            isAsce = !isAsce;
        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            if (isAsce)
                view.Filter = delegate(object obj) { return ((TestModelInfo)obj).Age > 30; };
            else
                view.Filter = null;
            isAsce = !isAsce;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            view.MoveCurrentToNext();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            view.MoveCurrentToPrevious();
        }
    }
}
